﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Permissions;

namespace BugTracker.InfoBox
{
    [Designer(typeof(InfoBoxDesigner))]
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:InfoBox runat=server></{0}:InfoBox>")]
    public class InfoBox : WebControl, INamingContainer
    {
        #region enum

        public enum MsgType : int
        {
            yellow, red, blue, green
        }

        #endregion

        #region properties

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(MsgType.green)]
        [Localizable(false)]
        public MsgType MessageType
        {
            get
            {
                return (MsgType?)ViewState["MessageType"] ?? MsgType.green;
            }

            set
            {
                ViewState["MessageType"] = value;
            }
        }

        #endregion

        #region method-override

        protected override void OnLoad(EventArgs e)
        {
            if (this.Page != null)
            {
                string js = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "BugTracker.InfoBox.Resources.infobox.js");
                string jquery = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "BugTracker.InfoBox.Resources.jquery-1.4.1.min.js");
                string css = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "BugTracker.InfoBox.Resources.infobox.css");

                HtmlLink cssLink = new HtmlLink();
                cssLink.Href = css;
                cssLink.ID = "infobox";
                cssLink.Attributes.Add("rel", "stylesheet");
                cssLink.Attributes.Add("type", "text/css");
                
                bool found = false;
                foreach (Control headctrl in Page.Header.Controls)
                {
                    if (headctrl.ID == cssLink.ID)
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    this.Page.Header.Controls.Add(cssLink);
                }

                if (!this.Page.ClientScript.IsClientScriptIncludeRegistered("jquery-1.4.1.min.js"))
                    this.Page.ClientScript.RegisterClientScriptInclude("jquery-1.4.1.min.js", jquery);

                if (!this.Page.ClientScript.IsClientScriptIncludeRegistered("infobox.js"))
                    this.Page.ClientScript.RegisterClientScriptInclude("infobox.js", js);
            }
            base.OnLoad(e);
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            string color = "green";
            switch (MessageType)
            {
                case MsgType.yellow:
                    color = "yellow";
                    break;
                case MsgType.green:
                    color = "green";
                    break;
                case MsgType.blue:
                    color = "blue";
                    break;
                case MsgType.red:
                    color = "red";
                    break;
            }
            string iconUrl = string.Empty;
            if (this.Page != null)
            {
                iconUrl =
                this.Page.ClientScript.GetWebResourceUrl(this.GetType(),
                string.Format("BugTracker.InfoBox.Resources.icon_close_{0}.gif", color));
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("<div id=\"table-content\">");
            sb.Append(string.Format("<!--  start message-{0} -->", color));
            sb.Append(string.Format("<div id=\"message-{0}\">", color));
            sb.Append("<table class=\"infobox\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">");
            sb.Append("<tr>");
            sb.Append(string.Format("<td class=\"{0}-left\">", color));
            sb.Append("&nbsp;");
            sb.Append(Text);
            sb.Append("</td>");
            sb.Append(string.Format("<td class=\"{0}-right\">", color));
            sb.Append(string.Format("<a class=\"close-{0}\">", color));
            sb.Append(string.Format("<img src=\"{0}\" alt=\"\" /></a>", iconUrl));
            sb.Append("</td>");
            sb.Append("</tr>");
            sb.Append("</table>");
            sb.Append("</div>");
            sb.Append("<!--  end message- -->");
            sb.Append("</div>");
            output.Write(sb.ToString());
        }

        #endregion
    }
}
